﻿using IBLL;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Model;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;

namespace work314API.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class UserController : ControllerBase
    {
        public UserController(IUserBLL bll,IWebHostEnvironment env)
        {
            Bll = bll;
            Env = env;
        }

        public IUserBLL Bll { get; }
        public IWebHostEnvironment Env { get; }

        [HttpPost]

        public async Task<int> AddHelp(HelpModel model)
        {
            return await Bll.AddHelp(model);
        }
        [HttpGet]
        public dynamic GetAllHelp(int PageIndex, int PageSize, string? Name)
        {
            return Bll.GetAllHelp(PageIndex, PageSize, Name);
        }
        [HttpGet]
        public dynamic GetAllFood(string? Name, int? FoodId)
        {
            return Bll.GetAllFood(Name, FoodId);
        }
        [HttpPost]
        public async Task<int> AddFood(FoodModel models)
        {
            return await Bll.AddFood(models);
        }
        [HttpGet]
        public Task<int> UpdDel(int id, bool isDel)
        {
            return Bll.UpdDel(id, isDel);
        }
        [HttpGet]
        public async Task<int> UpdState(string idlist)
        {
            return await Bll.UpdState(idlist);
        }
        [HttpPost]
        public IActionResult UpFile(IFormFile file)
        {
            var n = Guid.NewGuid().ToString();
            var path = Env.WebRootPath + "/img/" + n + file.FileName;
            using(FileStream fs = System.IO.File.Create(path))
            {
                file.CopyTo(fs);
                fs.Close();
            }
            return Ok("/img/" + n + file.FileName);
        }
        [HttpPost]  
        public List<FoodTypeModel> GetAllType()
        {
            return Bll.GetAllType();
        }
        [HttpGet]
        public IActionResult Checkinfo()
        {
            List<HelpModel> list = Bll.GetAlls();
            XSSFWorkbook wb = new XSSFWorkbook();
            ISheet sheat1 = wb.CreateSheet("康复理疗表");
            IRow row = sheat1.CreateRow(0);
            row.CreateCell(0).SetCellValue("编号");
            row.CreateCell(1).SetCellValue("名称");
            row.CreateCell(2).SetCellValue("是否删除");
            row.CreateCell(3).SetCellValue("状态");
            row.CreateCell(4).SetCellValue("服务时长");

            int num = 1;
            foreach (var item in list)
            {
                IRow row2 = sheat1.CreateRow(num++);
                row2.CreateCell(0).SetCellValue(item.Id);
                row2.CreateCell(1).SetCellValue(item.Name);
                row2.CreateCell(2).SetCellValue(item.isDel?"未删除":"已删除");
                row2.CreateCell(3).SetCellValue(item.State?"启用":"禁用");
                row2.CreateCell(4).SetCellValue(item.date.ToString("yyyy-MM-dd"));
            }
            byte[] bt =null;
            using (MemoryStream ms = new MemoryStream())
            {
                wb.Write(ms);
                bt = ms.ToArray();
            }
            return File(bt, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "康复理疗信息.xlsx");
        }
    }
}
